# -*- coding: UTF-8 -*-   

class QuestionPaser:

    '''构建实体节点'''
    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)

        return entity_dict

    '''解析主函数'''
    def parser_main(self, res_classify):
        #提取出实体
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        #提取出查询类型
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {}
            sql_['question_type'] = question_type
            sql = []
            # 查询课程所含知识单元
            # print(question_type)
            if question_type == 'zhishidanyuan':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))

            #查询课程简介
            elif question_type == 'jieshao':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #查询课程学时
            elif question_type == 'xueshi':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #查询课程先修课程
            elif question_type == 'xianxiukecheng':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
                # print(sql)
            #教学大纲
            elif question_type == 'dagang':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #查找课程知识点
            elif question_type == 'zhishidian':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #查找课程归属
            elif question_type == 'guishu':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #掌握程度
            elif question_type == 'zhangwochengdu':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSD'))
            #重要程度
            elif question_type == 'zhongyaochengdu':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSD'))
            #后继课程
            elif question_type == 'houjikecheng':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #前置课程
            elif question_type == 'qianzhikecheng':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #后继知识点
            elif question_type == 'houjizhishidian':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSD'))
            #前置知识点
            elif question_type == 'qianzhizhishidian':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSD'))
            #后继知识单元
            elif question_type == 'houjizhishidanyuan':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSDY'))
            #前置知识单元
            elif question_type == 'qianzhizhishidanyuan':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSDY'))
            #知识单元包含哪些知识点
            elif question_type == 'ZSDYZSD':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSDY'))
            #知识单元属于什么课程
            elif question_type == 'ZSDYKC':
                sql = self.sql_transfer(question_type, entity_dict.get('ZSDY'))
            #课程所含知识单元数量
            elif question_type == 'ZSDYSL':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            #课程所含知识点数量
            elif question_type == 'ZSDSL':
                sql = self.sql_transfer(question_type, entity_dict.get('kecheng'))
            if sql:
                sql_['sql'] = sql

                sqls.append(sql_)

        return sqls

    '''针对不同的问题，分开进行处理'''
    def sql_transfer(self, question_type, entities):
        if not entities:
            return []

        # 查询语句
        sql = []
        # 知识单元

        if question_type == 'zhishidanyuan':

            #sql=["match (m:Class)-[]->() where m.title='{0}' return m.rating,m.title".format(entities[0])]
            # "match(m:`课程知识单元内容信息`)-[r:`知识单元`]->(b:`知识单元`) where m.name='离散结构知识单元内容信息' RETURN b"
            # "match(m:`课程知识点内容信息`)-[r:`知识点`]->(b:`知识点`) where m.name=\"{0}\"'离散结构知识点内容信息' RETURN b"
            # "match(m:`课程知识单元内容信息`)where m.name=\"{0}\"  RETURN m"
            sql = ["match(m:`课程`)-[r:`课程所含知识单元`]->(b:`知识单元`) where m.name='{0}'  RETURN m.name,b.name".format(i) for i in entities]
        # 介绍
        elif question_type == 'jieshao':
            sql = ["match(m:课程)-[]->() where m.name='{0}' return m.name,m.jieshao".format(i) for i in entities]
        # 学时
        elif question_type == 'xueshi':
            sql = ["match(m:课程)-[]->() where m.name='{0}' return m.name,m.xueshi".format(i) for i in entities]
        # 教学大纲
        elif question_type == 'dagang':
            sql = ["match(m:课程) where m.name='{0}' return m.name,m.jiaoxuedagang".format(i) for i in entities]
        # 先修课程
        elif question_type == 'xianxiukecheng':
            sql = ["match(m:课程)-[r:`前置课程`]->(b) where m.name='{0}' return m.name,b.name".format(i) for i in entities]

        # 查找课程知识点
        elif question_type == 'zhishidian':
            sql = ["match(m:课程)-[r:`课程所含知识点`]->(b:知识点) where m.name=\"{0}\" return m.name,b.name".format(i) for i in entities]
        # 查找课程归属
        elif question_type == 'guishu':
            sql = ["match(m:课程)-[r:`所属专业`]->(b) where m.name=\"{0}\" return m.name,b.name".format(i) for i in entities]
        # 掌握程度
        elif question_type == 'zhangwochengdu':
            sql = ["match(m:知识点) where m.name='{0}' return m.name,m.掌握程度".format(i) for i in entities]
        # 重要程度
        elif question_type == 'zhongyaochengdu':
            sql = ["match(m:知识点) where m.name='{0}' return m.name,m.重要程度".format(i) for i in entities]

        # 后继课程
        elif question_type == 'houjikecheng':
            sql = ["match(m:课程)-[r:`后继课程`]->(b) where m.name=\"{0}\" return m.name,b.name".format(i) for i in entities]
        # 前置课程
        elif question_type == 'qianzhikecheng':
            sql = ["match(m:课程)-[r:`前置课程`]->(b) where m.name='{0}' return m.name,b.name".format(i) for i in entities]
        # 后继知识点
        elif question_type == 'houjizhishidian':
            sql = ["match(m:知识点)-[r:`后继知识点`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
        # 前置知识点
        elif question_type == 'qianzhizhishidian':
            sql = ["match(m:知识点)-[r:`前置知识点`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
        # 后继知识单元
        elif question_type == 'houjizhishidanyuan':
            sql = ["match(m:知识单元)-[r:`后继知识单元`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
            # print(sql)
        # 前置知识单元
        elif question_type == 'qianzhizhishidanyuan':
            sql = ["match(m:知识单元)-[r:`前置知识单元`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
        # 知识单元所含知识点
        elif question_type == 'ZSDYZSD':
            sql = ["match(m:知识单元)-[r:`知识点`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
        # 知识单元所属课程
        elif question_type == 'ZSDYKC':
            sql = ["match(m:课程)-[r:`课程所含知识单元`]->(b) where b.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
        # 知识单元所含知识点
        elif question_type == 'ZSDYSL':
            sql = ["match(m:课程)-[r:`课程所含知识单元`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]

        # 知识单元所属课程
        elif question_type == 'ZSDSL':
            sql = ["match(m:课程)-[r:`课程所含知识点`]->(b) where m.name=\"{0}\" return b.name,m.name".format(i) for i in entities]
        print(sql)
        return sql
    # print(sql_transfer('zhishidanyuan','离散结构，软件工程导论'))


if __name__ == '__main__':
    handler = QuestionPaser()
